/**
 * 维修费用结算维护
 * @author miaoyl
 * @date 2019/08/30
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_mmrw = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'MMRWDOCNO', width: 180, title: '单据ID', fixed: 'left', sort: true}
            , {field: 'MMRWSEQ', width: 80, title: '项次'}
            , {field: 'MMRWDOCDATE', width: 80, title: '单据日期',
                search: 'default',
                searchOpts: {type: 'daterange'}}
            , {field: 'MMRWLEGAL_NAME', width: 100, title: '法人组织',
                search: 'default', searchOpts: {icon: true}
            }
            , {field: 'MMRW001', width: 150, title: '配件编号'}
            , {field: 'GWAML003', width: 150, title: '品名',
                templet: function (data) {
                    if(data.GWAML003 != undefined){
                        return data.GWAML003;
                    }else if(data.MMRCL003 != undefined){
                        return data.MMRCL003;
                    }else {
                        return '';
                    }
                }
            }
            , {field: 'GWAML004', width: 80, title: '规格'}
            , {field: 'MMRW002', width: 80, title: '单位'}
            , {field: 'MMRW003', width: 80, title: '数量'}
            , {field: 'MMRW005', width: 80, title: '仓库'}
            , {field: 'MMRW016_NAME', width: 100, title: '人员',
                search: 'default', searchOpts: {icon: true}
            }
            , {field: 'MMRW017_NAME', width: 100, title: '部门',
                search: 'default', searchOpts: {icon: true}
            }
            , {
                field: 'MMRWACTI', width: 80, title: '状态',
                filterDatas: app.datas_('base_gwei025_acti'),
                search: 'default',searchOpts: {type: 'select', default: ''}
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-gwei024setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei024SettingAction,
        moduleName = "维修费用结算"
    ;

    var gwei024 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwei024',
                _searchs: [{
                    id: 'gwei024', tableId: 'gwei024_table',datas: _searchsArr.pushArray([
                        {
                            field: 'KEHU', title: '客户',
                            search: 'default',
                            searchOpts: {icon: true}
                        }
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwei024_edit', tpl: '/gwei024_edit'
                },{
                    dom: 'common_open_multi_baogong', tpl: 'common/_open_multi_baogong' //1
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window' //2
                },{
                    dom: 'gwei024_erp_result', tpl: '/gwei024_erp_result' //3
                },]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei024_table'),
                    url: g_ctxPath + 'crm/gwei024/getListData',
                    exportsUrl: g_ctxPath + 'crm/gwei024/exportsData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwei024_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwei024setting-action').innerHTML).render({}, function (string) {
                    gwei024SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwei024_table)', function (obj) {

                    layer.tips(gwei024SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if (obj.data.MMRWACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的维修费用结算才可以编辑！");
                                            return;
                                        }
                                        self._openTplFull({
                                            tag: 'gwei024_edit',
                                            tagData: {
                                                obj: obj,
                                                moduleName: moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '编辑',
                                                minmax: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if (obj.data.MMRWACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的维修费用结算才可以删除！");
                                            return;
                                        }
                                        self.del(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei024_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei024_edit',
                        tagData: {
                            obj: obj,
                            moduleName: moduleName,
                            is_check:true
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '详情',
                            minmax: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwei024_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei024_edit',
                                tagData: {
                                    is_add: true,
                                    moduleName: moduleName
                                },
                                openOpts: {
                                    title: moduleName + '新增',
                                    minmax: true
                                },
                                index: 0
                            });
                            break;
                        case 'del':
                            self.deleteDataBatch();
                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                        case 'transferReport':
                            self._openTplFull({
                                tag: 'common_open_multi_baogong',
                                tagData: {
                                    operation: 'transfer',
                                    cb: function (data) {
                                        var _xdata = [];
                                        data.forEach(function (v) {
                                            var _temp = app.fields_('MMRW_T');
                                            _temp.MMRW001 = v.MMRH003;
                                            _temp.MMRW002 = v.GWAM008;
                                            _temp.MMRW003 = v.MMRH004;
                                            _temp.MMRW011 = v.MMRHDOCNO;
                                            _temp.MMRW012 = v.MMRHSEQ;
                                            _temp.MMRW016 = v.MMRHUSER;
                                            _temp.MMRW017 = v.ORG_ID;
                                            _temp.MMRW015 = v.MMRG013;
                                            _xdata.push(_temp);
                                        });
                                        app.ajax({
                                            xurl: 'crm/gwei024/transferMmrghData',
                                            xdata: _xdata,
                                            loading: true,
                                            toJsonStr: true,
                                            cb: function (result) {
                                                if (result.ok) {
                                                    app.showSuccess("报工单转入成功,单号为：" + result.data.data.docno);
                                                    self.reloadTableCurr();
                                                }
                                            }
                                        });
                                    }
                                },
                                openOpts: {
                                    title: '报工单转入',
                                    maxmin: true
                                },
                                index: 1
                            });
                            break;
                        case 'transferERP':
                            self.transferERP();
                            break;
                        case 'restoreERP':
                            self.restoreERP();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwei024_table', where: table_mmrw});
        },

        del: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwei024/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName +  '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        deleteDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwei024_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('MMRW_T');
                    if (v.MMRWACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的结算单才可删除！");
                        flag = false;
                        return;
                    } else {
                        _temp.MMRWSENT = v.MMRWSENT;
                        _temp.MMRWDOCNO = v.MMRWDOCNO;
                        _temp.MMRWSEQ = v.MMRWSEQ;
                        _xdata.push(_temp);
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwei024/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否确认删除?"},
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwei024_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('MMRW_T');
                    if (v.MMRWACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的结算单可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.MMRWSENT = v.MMRWSENT;
                        _temp.MMRWDOCNO = v.MMRWDOCNO;
                        _temp.MMRWACTI = 'Y';
                        _xdata.push(_temp);
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwei024/updateStatus',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否确认审核?"},
                    okmsg: moduleName + '信息审核成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwei024_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('MMRW_T');
                    if (v.MMRWACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的结算单可以取消审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.MMRWSENT = v.MMRWSENT;
                        _temp.MMRWDOCNO = v.MMRWDOCNO;
                        _temp.MMRWACTI = 'N';
                        _xdata.push(_temp);
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwei024/updateStatus',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否取消审核?"},
                    okmsg: moduleName + '信息取消审核成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        transferERP:function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwei024_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('MMRW_T');
                    if (v.MMRWACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的结算单可以转订单！");
                        flag = false;
                        return;
                    } else {
                        _xdata.push(v.MMRWDOCNO);
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwei024/transferERP',
                    xdata: {datas: _xdata.toString()},
                    loading: true,
                    confirm: {msg: "是否确认生成?"},
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                            self._openTplFull({
                                tag: 'gwei024_erp_result',
                                tagData: {
                                    list: result.data.data
                                },
                                index: 3,
                                openOpts: {
                                    title: '转ERP订单结果',
                                    area: ['800px', '400px']
                                }
                            });
                        }
                    }
                });
            }
        },

        restoreERP: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwei024_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('MMRW_T');
                    if (v.MMRWACTI != 'Z') {
                        app.showWarning("只有状态为Z：已转订单的结算单可以撤销！");
                        flag = false;
                        return;
                    } else {
                        _xdata.push(v.MMRWDOCNO);
                    }
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(flag){
                app.ajax({
                    xurl: 'crm/gwei024/restoreERP',
                    xdata: {datas: _xdata.toString()},
                    loading: true,
                    confirm: {msg: "是否确认撤销?"},
                    cb: function (result) {
                        if (result.ok) {
                            app.showSuccess("还原ERP订单结果：" + result.data.data.docno);
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwei024',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'KEHU') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label:'客户编号',prop:'GWAK001', width: 150},
                                                {label:'客户简称',prop:'GWAKL003', width: 150},
                                                {label:'客户全称',prop:'GWAKL004', width: 150}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.KEHU = data.GWAK001;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'MMRWLEGAL_NAME') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_biz',
                                            columnList: [{label: '归属法人编号', prop: 'ID', width: '150'},
                                                {label: '归属法人名称', prop: 'NAME', width: '150'},
                                                {label: '营运中心编号', prop: 'BIZ_CODE', width: '150'},
                                                // {label: '营运中心名称', prop: 'BIZ_CODE_NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRWLEGAL_NAME = data.ID;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择法人组织',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'MMRW017_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysorg_id',
                                            columnList: [{label: '部门编号', prop: 'ID', width: '150'},
                                                {label: '部门名称', prop: 'NAME', width: '150'}]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRW017_NAME = data.ID;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择部门',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'MMRW016_NAME') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [{label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '人员名称', prop: 'NAME', width: '150'}]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRW016_NAME = data.ID;
                                        }
                                    },
                                    index: 2,
                                    openOpts: {
                                        title: '选择人员',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.MMRWDOCDATE': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.MMRWDOCDATE_START = newValue[0];
                                this.isearch.fields.MMRWDOCDATE_END = newValue[1];
                            } else {
                                this.isearch.fields.MMRWDOCDATE_START = '';
                                this.isearch.fields.MMRWDOCDATE_END = '';
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new gwei024();

});
